package com.iwuke.dance.util;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

import androidx.annotation.NonNull;

/**
 * @author: 魏从响
 * @date: 2021/9/1
 */
public class CornersTransform extends BitmapTransformation {
    private float radius;

    public CornersTransform(int radius) {
        this.radius = radius;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth,
                               int outHeight) {
        return cornersImage(pool, toTransform);
    }

    private Bitmap cornersImage(BitmapPool pool, Bitmap bitmapSource) {
        Bitmap tempBitmap = null;
        try {
            if (bitmapSource == null) {
                return null;
            }
            tempBitmap = pool.get(bitmapSource.getWidth(), bitmapSource.getHeight(),
                    Bitmap.Config.ARGB_8888);
            if (tempBitmap == null) {
                tempBitmap = Bitmap.createBitmap(bitmapSource.getWidth(),
                        bitmapSource.getHeight(), Bitmap.Config.ARGB_8888);
            }
            Canvas canvas = new Canvas(tempBitmap);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(bitmapSource, BitmapShader.TileMode.CLAMP,
                    BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);

            RectF rectF = new RectF(0f, 0f, bitmapSource.getWidth(),
                    bitmapSource.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tempBitmap;
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }

}